---
title: "Replication Data for: Will Voters Consider Long-Term Trends When State Crime Rates Spike?"
author: "G. Agustin Markarian"
date: "02/26/2025"
output:
  html_document: default
  pdf_document: default
  word_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(warning = FALSE)
knitr::opts_chunk$set(message = FALSE)

#Call required Packages
library(tidyverse)
library(stargazer)
library(readr)
library(dplyr)
library(ggpubr)
library(rstatix)
library(weights)
library(dotwhisker)
library(ggplot2)
library(gridExtra)
library(compareGroups)
library(nnet)
library(broom)
```

```{r datadownload}
#Download survey Data
d <- read_csv("EnglishSurveyWeightedNum.csv")[-c(1:2)]
d <- as.data.frame(d)
```

```{r dataclean1}
#rename randomization variables
colnames(d)[90:92] <- c("matchup_rand", "roe_rand", "crime_rand")

#droping all unused randomization variables
d <- select(d, -c(grep("_DO", colnames(d))))

#naming variables
colnames(d)[18:57] <- c(
  "vote_nov2022",
  "vc_gov",
  "vc_sen",
  "vc_ltgov",
  "vc_sos",
  "vc_con",
  "vc_tre",
  "vc_sspi",
  "vc_ag1.1",
  "vc_ic1.1",
  "vc_ag1.2",
  "vc_ic2.1",
  "vc_ag2.1",
  "vc_ic2.2",
  "vc_ag2.2",
  "vc_ic1.2",
  "attention1",
  "vc_pres",
  "recall_vc",
  "recall_satisfied",
  "recall_changes",
  "recall_reform1",
  "recall_reform2",
  "recall_reform3",
  "recall_reform4",
  "cd_betterplace",
  "cd_placetolive",
  "cd_peoplelikeme",
  "cd_moving",
  "misinfo1",
  "misinfo2",
  "misinfo3",
  "misinfo4",
  "county",
  "attention2",
  "ex1_important",
  "ex1_vote",
  "ex2_agapprove",
  "ex2_agvote",
  "ex2_prop47"
)

d <- d[-c(1:2),]
```

```{r dataclean2}
#Removing pre-lucid tests
d <- subset(d, as.POSIXct(d$StartDate) > as.Date("2022-06-11"))

#Removing test through lucid. "6" used in county to identify tests
d <- subset(d, !grepl("6" , county))
```

```{r dataclean3}
#Race
d$race_use <- car::recode(d$race,"1='white';2='black'; 3='other'; 4='asian'; 5='asian'; 6='asian'; 7='asian'; 8='asian'; 9='asian'; 10='asian'; 11:16='other'")
d$race_use <- ifelse(d$hispanic != "1", "hispanic", d$race_use)

d$black <- ifelse(d$race_use == "black", 1, 0)
d$asian <- ifelse(d$race_use == "asian", 1, 0)
d$hispanic <- ifelse(d$race_use == "hispanic", 1, 0)

#Gender
d$male <- ifelse(d$gender==1,1,0)
d$female <- ifelse(d$gender==2,1,0)

d$gender_use <- ifelse(d$male==1, "Male", NA)
d$gender_use <- ifelse(d$female==1, "Female", d$gender_use)
  
#Age
d$age1824 <- ifelse(d$age <= 24, 1, 0)
d$age2544 <- ifelse(d$age > 24 & d$age <=44, 1, 0)
d$age4564 <- ifelse(d$age > 44 & d$age <=64, 1, 0)
d$age65 <- ifelse(d$age <= 65, 1, 0)

d$age_group <- car::recode(d$age, "18:24='18-24'; 25:44='25-44'; 45:64='45-64'; 65:99='65+'")

#Ideology
d$ideology <- ifelse(d$ideology==8, NA, d$ideology)
d$ideology_use <- dplyr::recode(d$ideology, "1" = "Liberal",
                           "2" = "Liberal",
                           "3" = "Liberal",
                           "4" = "Moderate",
                           "5" = "Conservative",
                           "6" = "Conservative",
                           "7" = "Conservative",
                           .default = NA_character_)

#Income
d$incomeUnder25k <- ifelse(d$hhi <= 5, 1, 0)
d$income25k <- ifelse(d$hhi > 5 & d$hhi <= 10, 1, 0)
d$income50K <- ifelse(d$hhi > 10 & d$hhi <= 15, 1, 0)
d$income75k <- ifelse(d$hhi > 15 & d$hhi <= 20, 1, 0)
d$income100k <- ifelse(d$hhi > 20 & d$hhi <= 22, 1, 0)
d$incomeOver150k <- ifelse(d$hhi > 22 , 1, 0)

d$income_group <- car::recode(as.numeric(d$hhi), "c(1,2,3,4,5)='Under $25,000'; c(6,7,8,9,10)='$25,000 - $49,999'; c(11,12,13,14,15)='$50,000 - $74,999'; c(16,17,18,19,20)='$75,000 - $99,999'; c(21,22)='$100,000 - $149,999'; c(23,24,25,26)='Over $150,000'; '-3105'=NA")

d$incomeC <- as.numeric(car::recode(as.numeric(d$hhi), "'-3105'=NA"))

#Region
d$county <- tolower(d$county)

#PID
d$pid_use <- dplyr::recode(d$pid, "1"="Democrat", "3"="Independent", "2"="Republican", .default = NA_character_)

d$crime_treatment <- as.factor(ifelse(d$crime_rand=="CrimeTreatment","Long & Short Term Crime Trends", "Only Short Term Crime Trends"))

d <- d %>%
  mutate(vc_ag1.1 = ifelse(is.na(vc_ag1.1), vc_ag1.2, vc_ag1.1), 
            ag1_bonta = ifelse(vc_ag1.1 == 1, 1, 0),
            ag1_hochman = ifelse(vc_ag1.1 == 6 , 1, 0),
         vc_ag2.1 = ifelse(is.na(vc_ag2.1), vc_ag2.2, vc_ag2.1), 
            ag2_bonta = ifelse(vc_ag2.1 == 1 , 1, 0),
            ag2_early = ifelse(vc_ag2.1 == 8 , 1, 0),
         bonta_pre = ifelse(ag1_bonta == 1 | ag2_bonta == 1, 1, NA),
         notbonta_pre = ifelse(ag1_hochman == 1 | ag2_early == 1, 1, 0),
         notbonta_pre = ifelse(is.na(notbonta_pre ), 0, 1),
         bonta_pre = ifelse(notbonta_pre==1,0, bonta_pre),
         ex2_agapproveC = as.numeric(dplyr::recode(ex2_agapprove, "1" = "3", "4"="2", "5"="1", "6"="0", .default = NA_character_)),
         ex2_agapproveD = as.numeric(dplyr::recode(ex2_agapprove, "1" = "1", "4"="1", "5"="0", "6"="0", .default = NA_character_)),
         ex2_agvote_reelect = as.numeric(dplyr::recode(ex2_agvote, "1" = "1", "4"="0", "6"="0", .default = NA_character_)),
         ex2_prop47C = as.numeric(dplyr::recode(ex2_prop47, "1" = "1", "4"="0", .default = NA_character_)),
         MN.ex2_agapprovec = as.numeric(dplyr::recode(ex2_agapprove, "1" = "3", "4"="2", "5"="1", "6"="0", .default = "5")),
         MN.ex2_agvote_reelect = as.numeric(dplyr::recode(ex2_agvote, "1" = "1", "4"="0", "6"="0", .default = "2")),
         MN.ex2_prop47C = as.numeric(dplyr::recode(ex2_prop47, "1" = "1", "4"="0", .default = "2")),
         dif_bonta = ex2_agvote_reelect - bonta_pre)
```

# Approval Graphs

```{r }
ggplot(d, aes(fill = as.factor(crime_treatment),  x=as.factor(ex2_agapproveC))) + 
  scale_fill_manual(values = c("grey80", "grey30")) +
  labs(x="", fill="Treatment", y="", title = "Distribution of Approval for Attorney General Rob Bonta by Treatment") +
  scale_x_discrete(labels = c("Strongly\nDisapprove", "Disapprove", "Approve", "Strongly\nApprove"), na.translate = FALSE) + 
  theme_bw() +
  geom_bar(aes(y = (..count..)/tapply(..count.., ..x.. ,sum)[..x..]), stat="count",  position = "dodge") + 
          scale_y_continuous(labels=scales::percent) 
```

```{r}
d %>%
  group_by(crime_treatment) %>%
  summarise(n = n(),
            sd = sd(ex2_agapproveC, na.rm=TRUE),
                    ex2_agapproveC = weighted.mean(as.numeric(ex2_agapproveC), the.wts, na.rm=TRUE)) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1)) %>%
  ggplot( aes(x = as.factor(crime_treatment),  y=ex2_agapproveC)) + 
  geom_col(fill="grey60")  + 
  labs(x="", fill="Treatment", y="Mean Approval", title = "Mean Approval for Attorney General Rob Bonta by Treatment") +
  scale_x_discrete(labels = c("Long & Short Term Crime Trends", "Only Short Term Crime Trends"), na.translate = FALSE) + 
  theme_bw() +
    geom_errorbar( aes(x=as.factor(crime_treatment), ymin=ex2_agapproveC-ic, ymax=ex2_agapproveC+ic), width=0.4, colour="red", alpha=0.9, size=1.3)
```

```{r }
g1 <- ggplot(d, aes(fill = as.factor(crime_treatment),  x=as.factor(ex2_agapproveD ))) + 
  scale_fill_manual(values = c("grey80", "grey30")) +
  labs(x="", fill="Treatment", y="", subtitle = "Approval of Incumbent Attorney General") +
  scale_x_discrete(labels = c("Disapprove or\nStrongly Disapprove", "Approve or\nStrongly Approve"), na.translate = FALSE) + 
  theme_bw() +
  theme(legend.position="none")  +
  geom_bar(aes(y = (..count..)/tapply(..count.., ..x.. ,sum)[..x..]), stat="count",  position = "dodge")  + 
          scale_y_continuous(breaks = c(0, .2, .4, .6), limits = c(0, .6), labels=scales::percent)

```

```{r}
g1.2 <- d %>%
  group_by(crime_treatment) %>%
  summarise(n = n(),
            sd = sd(ex2_agapproveC, na.rm=TRUE),
                    ex2_agapproveC = weighted.mean(as.numeric(ex2_agapproveC), the.wts, na.rm=TRUE)) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1)) %>%
  ggplot( aes(x = as.factor(crime_treatment),  y=ex2_agapproveC)) + 
  geom_col(aes(fill = as.factor(crime_treatment)))  +
  labs(x="", fill="Treatment", y="", subtitle = "3.2 Approval of Incumbent Attorney General\n(Ordinal: 0-3)") +
  scale_x_discrete(labels = c("Long and Short\nTerm Crime Trends", "Only Short\nTerm Crime Trends"), na.translate = FALSE) + 
  scale_fill_manual(labels = c("Long and Short Term Crime Trends", "Only Short Term Crime Trends"), values = c("grey30", "grey70")) +
  theme_bw() +
  theme( plot.subtitle = element_text(size=10)) + 
    geom_errorbar( aes(x=as.factor(crime_treatment), ymin=ex2_agapproveC-ic, ymax=ex2_agapproveC+ic), width=0.4, colour="red", alpha=0.9, size=1.3) +
  theme(legend.position = "none") + 
          scale_y_continuous(breaks = c(0, 1, 2, 3), limits = c(0,3))


```

# Vote Choice Graphs

```{r }
g2 <- ggplot(d, aes(fill = as.factor(crime_treatment),  x=as.factor(ex2_agvote_reelect))) + 
  scale_fill_manual(values = c("grey80", "grey30")) +
  labs(x="", fill="Treatment", y="", subtitle = "Plan to Vote for Incumbent Attorney General") +
  scale_x_discrete(labels = c("Plans to Vote for\nAny Other Candidate", "Plans to Vote for\nIncumbent AG"), na.translate = FALSE) + 
  theme_bw() +
  theme(legend.position="none")   +
  geom_bar(aes(y = (..count..)/tapply(..count.., ..x.. ,sum)[..x..]), stat="count",  position = "dodge")  + 
          scale_y_continuous(breaks = c(0, .2, .4, .6), limits = c(0, .6), labels=scales::percent)

```

```{r }
d %>%
  group_by(crime_treatment) %>%
  summarise(n = n(),
            sd = sd(bonta_pre, na.rm=TRUE),
                    bonta_pre = weighted.mean(as.numeric(bonta_pre), the.wts, na.rm=TRUE)) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1)) %>%
  ggplot( aes(x = as.factor(crime_treatment),  y=bonta_pre)) + 
  geom_col(aes(fill = as.factor(crime_treatment)))  +
  labs(x="", fill="Treatment", y="", title = "Placebo Test: Plan to Vote for Incumbent Attorney General", subtitle = "Question Asked Prior to Treatment") +
  scale_x_discrete(labels = c("Long and Short\nTerm Crime Trends", "Only Short\nTerm Crime Trends"), na.translate = FALSE) + 
  scale_fill_manual(labels = c("Long and Short Term Crime Trends", "Only Short Term Crime Trends"), values = c("grey30", "grey70")) +
  theme_bw() +
  theme( plot.subtitle = element_text(size=10)) + 
    geom_errorbar( aes(x=as.factor(crime_treatment), ymin=bonta_pre-ic, ymax=bonta_pre+ic), width=0.4, colour="red", alpha=0.9, size=1.3) +
  theme(legend.position = "none") + 
          scale_y_continuous(breaks = c(0, .2, .4, .6, .8), limits = c(0, .8), labels=scales::percent)
```

```{r}
g2.2 <- d %>%
  group_by(crime_treatment) %>%
  summarise(n = n(),
            sd = sd(ex2_agvote_reelect, na.rm=TRUE),
                    ex2_agvote_reelect = weighted.mean(ex2_agvote_reelect, the.wts, na.rm=TRUE)) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1)) %>%
  ggplot( aes(x = as.factor(crime_treatment),  y=ex2_agvote_reelect)) + 
  geom_col(aes(fill = as.factor(crime_treatment)))  + 
  labs(x="", fill="Treatment", y="", subtitle = "3.3 Plan to Vote for Incumbent Attorney General\n(Dichotomous: 0/1)") +
  scale_x_discrete(labels = c("Long and Short\nTerm Crime Trends", "Only Short\nTerm Crime Trends"), na.translate = FALSE) + 
  scale_fill_manual(labels = c("Long and Short Term Crime Trends", "Only Short Term Crime Trends"), values = c("grey30", "grey70")) +
  theme_bw() +
  theme( plot.subtitle = element_text(size=10)) + 
    geom_errorbar( aes(x=as.factor(crime_treatment), ymin=ex2_agvote_reelect-ic, ymax=ex2_agvote_reelect+ic), width=0.4, colour="red", alpha=0.9, size=1.3) +
  theme(legend.position = "none") + 
          scale_y_continuous(breaks = c(0, .2, .4, .6, .8), limits = c(0, .8), labels=scales::percent)
```

```{r }
d %>%
  group_by(crime_treatment) %>%
  summarise(n = n(),
            sd = sd(dif_bonta, na.rm=TRUE),
                    dif_bonta = weighted.mean(as.numeric(dif_bonta), the.wts, na.rm=TRUE)) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1)) %>%
  ggplot( aes(x = as.factor(crime_treatment),  y=dif_bonta)) + 
  geom_col(aes(fill = as.factor(crime_treatment)))  +
  labs(x="", fill="Treatment", y="", title = "Change in Plan to Vote for Incumbent Attorney General", subtitle = "Difference Pre-Post Treatment") +
  scale_x_discrete(labels = c("Long and Short\nTerm Crime Trends", "Only Short\nTerm Crime Trends"), na.translate = FALSE) + 
  scale_fill_manual(labels = c("Long and Short Term Crime Trends", "Only Short Term Crime Trends"), values = c("grey30", "grey70")) +
  theme_bw() +
  theme( plot.subtitle = element_text(size=10)) + 
    geom_errorbar( aes(x=as.factor(crime_treatment), ymin=dif_bonta-ic, ymax=dif_bonta+ic), width=0.4, colour="red", alpha=0.9, size=1.3) +
  theme(legend.position = "none")
```

# Prop 47 Graphs

```{r }
g3 <- ggplot(d, aes(fill = as.factor(crime_treatment),  x=as.factor(ex2_prop47C))) + 
  scale_fill_manual(values = c("grey80", "grey30")) +
  labs(x="", fill="Treatment", y="", subtitle = "Support for Tough on Crime Policy") +
  scale_x_discrete(labels = c("Oppose Tough on\nCrime Policy", "Support Tough on\nCrime Policy"), na.translate = FALSE) + 
  theme_bw()   +
  geom_bar(aes(y = (..count..)/tapply(..count.., ..x.. ,sum)[..x..]), stat="count",  position = "dodge") + 
          scale_y_continuous(breaks = c(0, .2, .4, .6), limits = c(0, .6), labels=scales::percent)
```

```{r}
g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}

mylegend<-g_legend(g3)

g3 <- g3 + theme(legend.position="none")

lay = rbind(c(1,1,2,2),
            c(1,1,2,2),
            c(3,3,4,4),
            c(3,3,NA,NA))

p3 <- grid.arrange(g1,g2,g3,mylegend,
                   layout_matrix=lay)
```

```{r}
g3.2 <- d %>%
  group_by(crime_treatment) %>%
  summarise(n = n(),
            sd = sd(ex2_prop47C, na.rm=TRUE),
                    ex2_prop47C = weighted.mean(ex2_prop47C,  the.wts, na.rm=TRUE)) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1)) %>%
  ggplot( aes(x = as.factor(crime_treatment),  y=ex2_prop47C)) + 
  geom_col(aes(fill = as.factor(crime_treatment)))  + 
  labs(x="", fill="Expected Relationship", y="", subtitle = "3.1 Support for Tough-on-Crime Policy\n(Dichotomous: 0/1)") +
  scale_x_discrete(labels = c("Long and Short\nTerm Crime Trends", "Only Short\nTerm Crime Trends"), na.translate = FALSE) + 
  scale_fill_manual(labels = c("Expected: Lower", "Expected: Higher"), values = c("grey70", "grey30")) +
  theme_bw() +
    geom_errorbar( aes(x=as.factor(crime_treatment), ymin=ex2_prop47C-ic, ymax=ex2_prop47C+ic), width=0.4, colour="red", alpha=0.9, size=1.3) +
  theme( plot.subtitle = element_text(size=10)) + 
          scale_y_continuous(breaks = c(0, .2, .4, .6, .8), limits = c(0, .8), labels=scales::percent)
```

```{r fig.width=8}
mylegend<-g_legend(g3.2)

g3.2 <- g3.2 + theme(legend.position="none")

lay = rbind(c(1,1,2,2),
            c(1,1,2,2),
            c(3,3,4,4),
            c(3,3,4,4))

p3 <- grid.arrange(g3.2,g1.2,g2.2,mylegend,
                   layout_matrix=lay)
```

# Simple T-test

```{r}

#Weighted approval 4 point model
m1 <- wtd.t.test(d$ex2_agapproveC[d$crime_treatment=="Long & Short Term Crime Trends"], d$ex2_agapproveC[d$crime_treatment=="Only Short Term Crime Trends"], weight = d$the.wts[d$crime_treatment=="Long & Short Term Crime Trends"], weighty = d$the.wts[d$crime_treatment=="Only Short Term Crime Trends"])

#Piped and edited 4 point model
stat.test <- d %>%
  filter(!is.na(ex2_agapproveC)) %>%
  t_test(ex2_agapproveC  ~ crime_treatment, detailed = TRUE) %>%
  select(estimate , estimate1 , estimate2 , .y. , n1 , n2 , statistic , p , df) %>%
  mutate(estimate = m1$additional[1],
         estimate1 = m1$additional[2],
         estimate2 = m1$additional[3],
         statistic = m1$coefficients[1],
         df = m1$coefficients[2],
         p = m1$coefficients[3]) %>%
  add_significance() 


#Weighted approval dummy model
m2 <- wtd.t.test(d$ex2_agapproveD[d$crime_treatment=="Long & Short Term Crime Trends"], d$ex2_agapproveD[d$crime_treatment=="Only Short Term Crime Trends"], weight = d$the.wts[d$crime_treatment=="Long & Short Term Crime Trends"], weighty = d$the.wts[d$crime_treatment=="Only Short Term Crime Trends"])

#Piped and edited approval dummy model
stat.test <- d %>%
  filter(!is.na(ex2_agapproveD)) %>%
  t_test(ex2_agapproveD ~ crime_treatment, detailed = TRUE) %>%
  select(estimate , estimate1 , estimate2 , .y. , n1 , n2 , statistic , p , df) %>%
  mutate(estimate = m2$additional[1],
         estimate1 = m2$additional[2],
         estimate2 = m2$additional[3],
         statistic = m2$coefficients[1],
         df = m2$coefficients[2],
         p = m2$coefficients[3]) %>%
  add_significance() %>%
  bind_rows(stat.test)

#Weighted VC model
m3 <- wtd.t.test(d$ex2_agvote_reelect[d$crime_treatment=="Long & Short Term Crime Trends"], d$ex2_agvote_reelect[d$crime_treatment=="Only Short Term Crime Trends"], weight = d$the.wts[d$crime_treatment=="Long & Short Term Crime Trends"], weighty = d$the.wts[d$crime_treatment=="Only Short Term Crime Trends"])

#Piped and edited VC model
stat.test <- d %>%
  filter(!is.na(ex2_agvote_reelect)) %>%
  t_test(ex2_agvote_reelect ~ crime_treatment, detailed = TRUE) %>%
  select(estimate , estimate1 , estimate2 , .y. , n1 , n2 , statistic , p , df) %>%
    mutate(estimate = m3$additional[1],
         estimate1 = m3$additional[2],
         estimate2 = m3$additional[3],
         statistic = m3$coefficients[1],
         df = m3$coefficients[2],
         p = m3$coefficients[3]) %>%
  add_significance() %>%
  bind_rows(stat.test)

#Weighted prop47 model
m4 <- wtd.t.test(d$ex2_prop47C[d$crime_treatment=="Long & Short Term Crime Trends"], d$ex2_prop47C[d$crime_treatment=="Only Short Term Crime Trends"], weight = d$the.wts[d$crime_treatment=="Long & Short Term Crime Trends"], weighty = d$the.wts[d$crime_treatment=="Only Short Term Crime Trends"])

#Piped and edited VC model
stat.test <- d %>%
  filter(!is.na(ex2_prop47C)) %>%
  t_test(ex2_prop47C ~ crime_treatment, detailed = TRUE) %>%
  select(estimate , estimate1 , estimate2 , .y. , n1 , n2 , statistic , p , df) %>%
    mutate(estimate = m4$additional[1],
         estimate1 = m4$additional[2],
         estimate2 = m4$additional[3],
         statistic = m4$coefficients[1],
         df = m4$coefficients[2],
         p = m4$coefficients[3]) %>%
  add_significance() %>%
  bind_rows(stat.test)

stat.test <- stat.test %>%
  mutate(.y. = c("Support Repealing Prop 47", "Plan to Vote for AG Rob Bonta", "Approval (Dummy) of AG Rob Bonta",  "Approval (4-point Scale) of AG Rob Bonta")) %>%
  select(.y. , estimate , estimate1 , estimate2 ,  n1 , n2 , statistic ,  df, p , p.signif)

colnames(stat.test) <- c("DV", "Difference", "LT_CT_Mean", "ST_CT_Mean", "N_Group1", "N_Group2", "T_Statistic", "DF", "P-Val", "P-Sig")

knitr::kable(stat.test, format = "simple", digits=3, caption = "Difference in Means by Treatment")


#Weighted Placebo Model model
m5 <- wtd.t.test(d$bonta_pre[d$crime_treatment=="Long & Short Term Crime Trends"], d$bonta_pre[d$crime_treatment=="Only Short Term Crime Trends"], weight = d$the.wts[d$crime_treatment=="Long & Short Term Crime Trends"], weighty = d$the.wts[d$crime_treatment=="Only Short Term Crime Trends"])
```

# Simple LM Model

```{r}
d$crime_treatment <- gsub("\\&", "and", d$crime_treatment)
d$crime_treatment <- factor(d$crime_treatment, levels = c("Only Short Term Crime Trends", "Long and Short Term Crime Trends"))
```

```{r}
m1 <- lm(as.numeric(ex2_agapproveC ) ~ as.factor(crime_treatment),d, weights = the.wts)

m2 <- lm(as.numeric(ex2_agvote_reelect ) ~ as.factor(crime_treatment),d, weights = the.wts)

m3 <- lm(as.numeric(ex2_prop47C ) ~ as.factor(crime_treatment),d, weights = the.wts)
```

```{r results='asis'}
stargazer(m3,m1,m2,
          column.labels = c("Support Repealing Prop 47", "Approval of AG Rob Bonta", "Plan to Vote for AG Rob Bonta"), 
          dep.var.labels.include = FALSE , 
          column.sep.width = "1pt", 
          type = "html", 
          font.size = "small", 
          header=FALSE, 
          omit.stat = c("f", "ser"), 
          float = FALSE,
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          out="table_simple.doc")
```

# Simply Logit Model

```{r}
m1.l <- glm(as.numeric(ex2_agapproveD ) ~ as.factor(crime_treatment),d, weights = the.wts, family = "binomial")

m2.l <- glm(as.numeric(ex2_agvote_reelect ) ~ as.factor(crime_treatment),d, weights = the.wts, family = "binomial")

m3.l <- glm(as.numeric(ex2_prop47C ) ~ as.factor(crime_treatment),d, weights = the.wts, family = "binomial")
```

```{r results='asis'}
stargazer(m3.l,m1.l,m2.l,
          column.labels = c("Support Repealing Prop 47", "Approval of AG Rob Bonta", "Plan to Vote for AG Rob Bonta"), 
          dep.var.labels.include = FALSE , 
          column.sep.width = "1pt", 
          type = "html", 
          font.size = "small", 
          header=FALSE, 
          omit.stat = c("f", "ser"), 
          float = FALSE,
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          out="table_simpleLOGIT.doc")
```

# Simple Interaction with PID

```{r}

m4 <- lm(as.numeric(ex2_agapproveC ) ~ as.factor(crime_treatment)*pid_use,d, weights = the.wts)

m5 <- lm(as.numeric(ex2_agvote_reelect ) ~ as.factor(crime_treatment)*pid_use,d, weights = the.wts)

m6 <- lm(as.numeric(ex2_prop47C ) ~ as.factor(crime_treatment)*pid_use,d, weights = the.wts)
```

```{r results='asis'}
stargazer(m6, m4, m5, 
          column.labels = c("Support Repealing Prop 47", "Approval of AG Rob Bonta", "Plan to Vote for AG Rob Bonta"), 
          dep.var.labels.include = FALSE , 
          column.sep.width = "1pt", 
          type = "html", 
          font.size = "small", 
          header=FALSE, 
          omit.stat = c("f", "ser"), 
          float = FALSE,
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          out="table_simple_interactions.doc")
```

# Simple Logit Interaction with PID 

```{r}

m4.l <- glm(as.numeric(ex2_agapproveD ) ~ as.factor(crime_treatment)*pid_use,d, weights = the.wts, family = "binomial")

m5.l <- glm(as.numeric(ex2_agvote_reelect ) ~ as.factor(crime_treatment)*pid_use,d, weights = the.wts, family = "binomial")

m6.l <- glm(as.numeric(ex2_prop47C ) ~ as.factor(crime_treatment)*pid_use,d, weights = the.wts, family = "binomial")
```

```{r results='asis'}
stargazer(m6.l, m4.l, m5.l, 
          column.labels = c("Support Repealing Prop 47", "Approval of AG Rob Bonta", "Plan to Vote for AG Rob Bonta"), 
          dep.var.labels.include = FALSE , 
          column.sep.width = "1pt", 
          type = "html", 
          font.size = "small", 
          header=FALSE, 
          omit.stat = c("f", "ser"), 
          float = FALSE,
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          out="table_simple_interactionsLOGIT.doc")
```

```{r}
#Plot
m1df <- broom::tidy(m1) %>% 
  mutate(model = "Model 1")

m2df <- broom::tidy(m2) %>% 
  mutate(model = "Model 2")

m3df <- broom::tidy(m3) %>% 
  mutate(model = "Model 3")

m4df <- broom::tidy(m4) %>% 
  mutate(model = "Model 4")

m5df <- broom::tidy(m5) %>% 
  mutate(model = "Model 5")

m6df <- broom::tidy(m6) %>% 
  mutate(model = "Model 6")


models1 <- rbind(m1df, m4df) %>%
  relabel_predictors('as.factor(crime_treatment)Long and Short Term Crime Trends'="Long & Short Term\nCrime Trends",
                     pid_useIndependent = "Independent",
                     pid_useRepublican = "Republican",
                     "as.factor(crime_treatment)Long and Short Term Crime Trends:pid_useRepublican" = "Long & Short Term\nCrime Trends*Republican",
                     "as.factor(crime_treatment)Long and Short Term Crime Trends:pid_useIndependent" = "Long & Short Term\nCrime Trends*Independent")

models2 <- rbind(m2df, m5df) %>%
  relabel_predictors('as.factor(crime_treatment)Long and Short Term Crime Trends'="L&S Crime Trends",
                     pid_useIndependent = "Independent",
                     pid_useRepublican = "Republican",
                     "as.factor(crime_treatment)Long and Short Term Crime Trends:pid_useRepublican" = "Long & Short Term\nCrime Trends*Republican",
                     "as.factor(crime_treatment)Long and Short Term Crime Trends:pid_useIndependent" = "Long & Short Term\nCrime Trends*Independent")

models3 <- rbind(m3df, m6df) %>%
  relabel_predictors('as.factor(crime_treatment)Long and Short Term Crime Trends'="Long & Short Term\nCrime Trends",
                     pid_useIndependent = "Independent",
                     pid_useRepublican = "Republican",
                     "as.factor(crime_treatment)Long and Short Term Crime Trends:pid_useRepublican" = "Long & Short Term\nCrime Trends*Republican",
                     "as.factor(crime_treatment)Long and Short Term Crime Trends:pid_useIndependent" = "Long & Short Term\nCrime Trends*Independent")

models3$model <- factor(models3$model, levels = c("Model 3", "Model 6"))

g1 <- dwplot(models1, ci = 0.95,
       vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2
       ), dot_args = list(aes(shape = model))) +
    theme_bw(base_size = 11) + xlab("") + ylab("") +
    theme(
        plot.title = element_text(face = "bold"),
        legend.position = c(0.007, 0.01),
        legend.justification = c(0, 0),
        legend.background = element_rect(colour = "grey80"),
        legend.title.align = .5
    ) +
    labs(subtitle = "4.2 Approve of Incumbent\nAttorney General") +
    scale_colour_grey(
        start = .1,
        end = .7,
        breaks = c("Model 1", "Model 4"),
        name = "Model",
        labels = c("Pooled Model", "Interaction Model")
    ) +
    scale_shape_discrete(
          breaks = c("Model 1", "Model 4"),
        name = "Model",
        labels = c("Pooled Model", "Interaction Model")
    ) +
        theme(legend.position = "none", axis.text.y = element_blank(), plot.subtitle = element_text(size = 9))


g2 <- dwplot(models2, ci = 0.95,
       vline = geom_vline(
         xintercept = 0,
         colour = "grey60",
         linetype = 2
       ), dot_args = list(aes(shape = model))) +
  theme_bw(base_size = 11) + xlab("") + ylab("") +
  labs(subtitle = "4.3 Vote for Incumbent\nAttorney General") +
    scale_x_continuous(limits = c(-0.9,0.25)) +
    scale_colour_grey(
        start = .1,
        end = .7,
        breaks = c("Model 1", "Model 4"),
        name = "Model",
        labels = c("Pooled", "Interaction")
    ) +
    scale_shape_discrete(
          breaks = c("Model 1", "Model 4"),
        name = "Model",
        labels = c("Pooled", "Interaction")
    ) +
    guides(
        shape = guide_legend(""), 
        colour = guide_legend("")) +
        theme(legend.position = "none", axis.text.y = element_blank(), plot.subtitle = element_text(size = 9))

g3 <- dwplot(models3, ci = 0.95,
       vline = geom_vline(
         xintercept = 0,
         colour = "grey60",
         linetype = 2
       ), dot_args = list(aes(shape = model))) +
  theme_bw(base_size = 11) + xlab("") + ylab("") +
  labs(subtitle = "4.1 Support for Tough-\non-Crime Policy") +
  scale_x_continuous(limits = c(-0.9,0.25)) +
    scale_colour_grey(
        start = .1,
        end = .7,
        breaks = c("Model 3", "Model 6"),
        name = "Model",
        labels = c("Pooled", "Interaction")
    ) +
    scale_shape_discrete(
          breaks = c("Model 3", "Model 6"),
        name = "Model",
        labels = c("Pooled", "Interaction")
    )  +
    guides(
        shape = guide_legend(""), 
        colour = guide_legend("")) +
        theme(legend.direction="horizontal", plot.subtitle = element_text(size = 9))


mylegend<-g_legend(g3)

g3 <- g3 + theme(legend.position="none")

lay = rbind(c(1,1,2,3),
            c(1,1,2,3),
            c(1,1,2,3),
            c(1,1,2,3),
            c(1,1,2,3),
            c(1,1,2,3),
            c(1,1,2,3),
            c(1,1,2,3),
            c(1,1,2,3),
            c(NA,4,4,NA))

p3 <- grid.arrange(g3, g1,g2,mylegend, layout_matrix=lay)

p3
```

# Multinomial logit

```{r}
m1.mn <- multinom(MN.ex2_agapprovec ~ as.factor(crime_treatment), d, weights = the.wts)

tidy(m1.mn)

m2.mn <- multinom(MN.ex2_agvote_reelect ~ as.factor(crime_treatment), d, weights = the.wts)

tidy(m2.mn)

m3.mn <- multinom(MN.ex2_prop47C ~ as.factor(crime_treatment), d, weights = the.wts)

tidy(m3.mn)
```

# Complete LM Model

* Age = continuous 
* Income = ordinal (7) (treated as continuous)
* Ideology = ordinal (26) (treated as continuous)
* Education = ordinal (4) (treated as continuous)
* Pid = nominal
* gender = dummy

```{r}
m1 <- lm(as.numeric(ex2_agapproveC ) ~ as.factor(crime_treatment) + pid_use + ideology + male + age + incomeC + educ_cat + black + asian + hispanic,d, weights = the.wts)

m2 <- lm(as.numeric(ex2_agvote_reelect ) ~ as.factor(crime_treatment) + pid_use + ideology + male + age + incomeC + educ_cat + black + asian + hispanic,d, weights = the.wts)

m3 <- lm(as.numeric(ex2_prop47C ) ~ as.factor(crime_treatment) + pid_use + ideology + male + age + incomeC + educ_cat + black + asian + hispanic,d, weights = the.wts)
```

```{r results='asis'}
stargazer(m3,m1,m2,
          column.labels = c("Support Repealing Prop 47", "Approval of AG Rob Bonta", "Plan to Vote for AG Rob Bonta"), 
          dep.var.labels.include = FALSE , 
          column.sep.width = "1pt", 
          type = "html", 
          font.size = "small", 
          header=FALSE, 
          omit.stat = c("f", "ser"), 
          float = FALSE,
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          out="table_controls.doc")
```

# Complete Interaction Model

```{r}
m4 <- lm(as.numeric(ex2_agapproveC ) ~ as.factor(crime_treatment)*pid_use + ideology + male + age + incomeC + educ_cat + black + asian + hispanic,d, weights = the.wts)

m5 <- lm(as.numeric(ex2_agvote_reelect ) ~ as.factor(crime_treatment)*pid_use + ideology + male + age + incomeC + educ_cat + black + asian + hispanic,d, weights = the.wts)

m6 <- lm(as.numeric(ex2_prop47C ) ~ as.factor(crime_treatment)*pid_use + ideology + male + age + incomeC + educ_cat + black + asian + hispanic,d, weights = the.wts)
```

```{r results='asis'}
stargazer(m6,m4,m5,
          column.labels = c("Support Repealing Prop 47", "Approval of AG Rob Bonta", "Plan to Vote for AG Rob Bonta"), 
          dep.var.labels.include = FALSE , 
          column.sep.width = "1pt", 
          type = "html", 
          font.size = "small", 
          header=FALSE, 
          omit.stat = c("f", "ser"), 
          float = FALSE,
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          out="table_controls_interactions.doc")
```

# Balance Table

```{r}
compare <- compareGroups(crime_treatment ~  pid_use + ideology + male + age + incomeC + educ_cat + black + asian + hispanic, data = d)
tab1 <- createTable(compare)
tab1

export2md(tab1, caption="Table SI-XX: Balance Table", format="html")
```

# Additional test requested by reviewer regarding other experiment (unrelated) included in PAP

```{r}
m1 <- lm(as.numeric(ex2_agapproveC ) ~ as.factor(crime_treatment) + as.factor(roe_rand),d, weights = the.wts)
 
m2 <- lm(as.numeric(ex2_agvote_reelect ) ~ as.factor(crime_treatment) + as.factor(roe_rand),d, weights = the.wts)

m3 <- lm(as.numeric(ex2_prop47C ) ~ as.factor(crime_treatment) + as.factor(roe_rand),d, weights = the.wts)
```


```{r results='asis'}
stargazer(m3,m1,m2,
          column.labels = c("Support Repealing Prop 47", "Approval of AG Rob Bonta", "Plan to Vote for AG Rob Bonta"), 
          dep.var.labels.include = FALSE , 
          column.sep.width = "1pt", 
          type = "html", 
          font.size = "small", 
          header=FALSE, 
          omit.stat = c("f", "ser"), 
          float = FALSE,
          star.char = c("+", "*", "**", "***"),
          star.cutoffs = c(.1, .05, .01, .001),
          out="table_controls_conditionalaccept_robust1.doc")
```